<?php

	class bLinqFile extends bLinqStore implements IbLinqStore {
		
		public function __construct($sPath) {
			
			$this->sFileStorePath =$sPath;
		}
		
		public function Add($oLinq, $aInsert =null) {

			$aInsert =$oLinq->RawKeyValue();
			$aQuery =$oLinq->RawQuery();
			
			$sFrom =dirname($this->sFileStorePath).'/'.$aQuery['from'].'.store.txt';
			
			if(!file_exists($sFrom)) { 
				
				$sFields =strtolower(self::AsCsv(array_keys($aInsert)))."\n";
				file_put_contents($sFrom, $sFields); 
			}
			
			$sValues =self::AsCsv(array_values($aInsert))."\n";
			file_put_contents($sFrom, $sValues, FILE_APPEND);

			return $this;
		}
		
		public function Select($oLinq, $aSelect =null) {
			
			$aQuery =$oLinq->RawQuery();
			$sFrom =dirname($this->sFileStorePath).'/'.$aQuery['from'].'.store.txt';
			
			$aFile =array_map('str_getcsv', file($sFrom));
			$aField =array_flip(array_map('strtolower', array_shift($aFile))); //Grab the first line
			
			$oResult =new bLinqResult();
			foreach($aFile as $aLine) {
				
				$aMerged =array_merge($aQuery['where'], $this->aQuery['where']);
				foreach($aMerged as $sKey => $sValue) {
					
					if(isset($aField[$sKey])) {
						
						switch($aMerged[$sKey]) {
							
							case "=":
								
								if($aLine[$aField[$sKey]] ==$aMerged[$sKey]["="]) {
									
									$oResult->Add($aLine);
								}
							break;
							case ">":
								
								if($aLine[$aField[$sKey]] >$aMerged[$sKey][">"]) {
									
									$oResult->Add($aLine);
								}
							break;
							case "<":
								
								if($aLine[$aField[$sKey]] <$aMerged[$sKey]["<"]) {
									
									$oResult->Add($aLine);
								}
							break;
							case "%":
								
								if(preg_match($aMerged[$sKey]["%"], $aLine[$aField[$sKey]])) {
									
									$oResult->Add($aLine);
								}
							break;
						}
					}
				}
			}
	
			return $oResult;
		}
		
		public function Equal($oLinq, $mValue) {
		
			$aQuery =$oLinq->RawQuery();
			unset($aQuery['where']["="]);
			
			$sKey =end(array_keys($aQuery['where']));
			$this->aQuery['where'][$sKey] =array('='=>$mValue);
			
			return $oLinq;
		}
		
		public function GreaterThan($oLinq, $mValue) {
			
			$aQuery =$oLinq->RawQuery();
			unset($aQuery['where'][">"]);
			
			$sKey =end(array_keys($aQuery['where']));
			$this->aQuery['where'][$sKey] =array('>'=>$mValue);
						
			return $oLinq;
		}
		
		public function LessThan($oLinq, $mValue) {
			
			$aQuery =$oLinq->RawQuery();
			unset($aQuery['where']["<"]);
			
			$sKey =end(array_keys($aQuery['where']));
			$this->aQuery['where'][$sKey] =array('<'=>$mValue);
			
			return $oLinq;
		}
		
		public function StartsWith($oLinq, $mValue) {
			
			$aQuery =$oLinq->RawQuery();
			unset($aQuery['where']["-%"]);
			
			$sKey =end(array_keys($aQuery['where']));
			$sExpression ='/'.preg_quote(str_replace(array('?', '%'), array('.', '*'), $mValue)).'/';
			$this->aQuery['where'][$sKey] =array('%'=>$sExpression);
			
			return $oLinq;
		}
		
		public function Contains($oLinq, $mValue) {
			
			$aQuery =$oLinq->RawQuery();
			unset($aQuery['where']["%"]);
			
			$sKey =end(array_keys($aQuery['where']));
			$sExpression ='/'.preg_quote(str_replace(array('?', '%'), array('.', '*'), $mValue)).'/';
			$this->aQuery['where'][$sKey] =array('%'=>$sExpression);
			
			return $oLinq;
		}
		
		public function EndsWith($oLinq, $mValue) {
			
			$aQuery =$oLinq->RawQuery();
			unset($aQuery['where']["%-"]);
			
			$sKey =end(array_keys($aQuery['where']));
			$sExpression ='/'.preg_quote(str_replace(array('?', '%'), array('.', '*'), $mValue)).'/';
			$this->aQuery['where'][$sKey] =array('%'=>$sExpression);
			
			return $oLinq;
		}
	}
	
?>